﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost
{
    public class ApiErrorResult : IHttpActionResult
    {
        private HttpRequestMessage Request { get; set; }

        private string Content { get; set; }

        public ApiErrorResult(string Content, HttpRequestMessage httpRequest)
        {
            this.Content = Content;
            this.Request = httpRequest;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response =
                          new HttpResponseMessage(HttpStatusCode.InternalServerError);
            response.Content = new StringContent(Content);
            response.RequestMessage = Request;
            return Task.FromResult(response);
        }
    }

    public class ApiFailResult : IHttpActionResult
    {
        private HttpResponseMessage Response { get; set; }
        public ApiFailResult(HttpResponseMessage httpResponse)
        {
            this.Response = httpResponse;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            return Task.FromResult(Response);
        }

    }

    public class ApiOkResult: IHttpActionResult
    {
        private HttpRequestMessage Request { get; set; }

        private string Content { get; set; }

        public ApiOkResult(string Content, HttpRequestMessage httpRequest)
        {
            this.Content = Content;
            this.Request = httpRequest;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response =
                          new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(Content);
            response.RequestMessage = Request;
            return Task.FromResult(response);
        }
    }

    public class ImageResult : IHttpActionResult
    {
        private byte[] imgbytes;
        private string mediaType;
        public ImageResult(byte[] imgbytes, string mediaType)
        {
            this.imgbytes = imgbytes;
            this.mediaType = mediaType;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(imgbytes)
            };
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
            return Task.FromResult(response);
        }
    }
}
